using System;
using System.Data;
using System.Windows.Forms;
namespace mywine.Class
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CStore : TUnit.Class.Class
    {
        #region Declares
        int _storeid = -1;
        string _storename = "";
        double _locationX = 0.0;
        double _locationY = 0.0;
        #endregion

        #region Constructors
        public CStore()
        {
        }
        public CStore(TUnit.Class.CLogin Login)
        {
            base.Login = Login;
        }
        public CStore(TUnit.Class.CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBStore db = new AccDBStore())
            {
                this.Populate((CStore)db.GetClass(Login, ObjId));
            }
        }
        public CStore(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CStore(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBStore db = new AccDBStore())
            {
                this.Populate((CStore)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(TUnit.Data.IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("storeid") >= 0)
                    {
                        this._storeid = (dr["storeid"] != DBNull.Value ? Convert.ToInt32(dr["storeid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_stores ADD storeid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("storename") >= 0)
                    {
                        this._storename = (dr["storename"] != DBNull.Value ? Convert.ToString(dr["storename"]) : "").Trim();
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_stores ADD storename [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("locationX") >= 0)
                    {
                        this._locationX = (dr["locationX"] != DBNull.Value ? Convert.ToDouble(dr["locationX"]) : 0.0);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_stores ADD locationX [numeric](18, 0) NULL ");
                    //}
                    if (dr.Table.Columns.IndexOf("locationY") >= 0)
                    {
                        this._locationY = (dr["locationY"] != DBNull.Value ? Convert.ToDouble(dr["locationY"]) : 0.0);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_stores ADD locationY [numeric](18, 0) NULL ");
                    //}
                }
                catch { }
            }
        }
        public void Populate(CStore Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._storeid = Obj.storeid;
                    this._storename = Obj.storename;
                    this._locationX = Obj.locationX;
                    this._locationY = Obj.locationY;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBStore db = new AccDBStore())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(TUnit.Data.IProvider p)
        {
            using (AccDBStore db = new AccDBStore())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBStore db = new AccDBStore())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(TUnit.Data.IProvider p)
        {
            using (AccDBStore db = new AccDBStore())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string toString()
        {
            return
                this.storeid.ToString() + "|"
                + this.storename + ""; 
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Collection"></param>
        /// <returns></returns>
        public static string toString(TUnit.Class.CCollection Collection)
        {
            string str = "";
            if (Collection != null)
            {
                for (int i = 0; i < Collection.Count; i++)
                {
                    CStore o = (CStore)Collection[i];
                    str += "@STORE";
                    str += o.toString();
                }
            }
            if (str.StartsWith("@STORE"))
                str = str.Substring(6);

            return str;
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CStore obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CStore Store);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CStore Store);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CStore Store);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CStore Store);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CStore obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int storeid
        {
            get { return this._storeid; }
            set
            {
                if (this._storeid != value)
                {
                    this._storeid = value;
                }
            }
        }
        public string storename
        {
            get { return this._storename; }
            set
            {
                if (this._storename != value)
                {
                    this._storename = value;
                }
            }
        }
        public double locationX
        {
            get { return this._locationX; }
            set
            {
                if (this._locationX != value)
                {
                    this._locationX = value;
                }
            }
        }
        public double locationY
        {
            get { return this._locationY; }
            set
            {
                if (this._locationY != value)
                {
                    this._locationY = value;
                }
            }
        }

        public TUnit.Class.CCollection _Collection = null;
        public TUnit.Class.CCollection Collection
        {
            get
            {
                if (this._Collection == null)
                {
                    using (AccDBWineColor db = new AccDBWineColor())
                    {
                        TUnit.Class.CCollection MyCollection = new TUnit.Class.CCollection();
                        db.GetCollection(this, MyCollection);


                        this._Collection = MyCollection;
                    }
                }
                return this._Collection;
            }
        }
        #endregion
    }
    #region Class AccDB
    public class AccDBStore : TUnit.Class.AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBStore()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(TUnit.Class.CLogin Login, ComboBox cmb, string extName)
        {
            TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT storeid, StoreName";
                sql += " FROM  tbl_stores";
                sql += " WHERE (storeid >= 0)";
                sql += " ORDER BY StoreName";
                DataTable table = p.OpenTable(sql, "tbl_stores");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["StoreName"] = extName;
                        row["storeid"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "StoreName";
                    cmb.ValueMember = "storeid";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override TUnit.Class.Class GetClass(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, object ObjId)
        {
            try
            {
                if (ObjId is string)
                {
                    string sql = "SELECT * FROM tbl_stores WHERE (storename = N'" + ObjId + "')";
                    DataTable table = p.OpenTable(sql, "tbl_stores");
                    if ((table != null) && (table.Rows.Count > 0))
                    {
                        return new CStore(Login, p, table.Rows[0]);
                    }
                }
                else
                {
                    string sql = "SELECT * FROM tbl_stores WHERE (storeid = " + ObjId + ")";
                    DataTable table = p.OpenTable(sql, "tbl_stores");
                    if ((table != null) && (table.Rows.Count > 0))
                    {
                        return new CStore(Login, p, table.Rows[0]);
                    }
                }

            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(TUnit.Data.IProvider p, TUnit.Class.Class obj)
        {
            CStore Store = (CStore)obj;
            string sql = "SELECT * FROM tbl_stores";
            sql += " WHERE storeid =" + Store.storeid + "";
            //sql +=" AND CompanyId =" + Store.CompanyId + "";
            DataTable dt = p.OpenTable(sql, "tbl_stores");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && Store.storeid != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    Store.storeid = Convert.ToInt32(p.InitIDKey("StoreId", "tbl_stores", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["storeid"] = Store.storeid;
                row["storename"] = Store.storename;
                row["locationX"] = Store.locationX;
                row["locationY"] = Store.locationY;
                if (!p.UpdateTable("tbl_stores", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(TUnit.Data.IProvider p, TUnit.Class.Class obj)
        {
            CStore Store = (CStore)obj;
            string sql = "DELETE FROM tbl_stores";
            sql += " WHERE StoreId =" + Store.storeid + "";
            //sql +=" AND CompanyId =" + Store.CompanyId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, TUnit.Class.CCollection Collection)
        {
            string sql = "SELECT * FROM tbl_stores ORDER BY storename";
            DataTable table = p.OpenTable(sql, "tbl_stores");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CStore o = new CStore(Login, p, row);
                    if(o.storeid != -1)
                        Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(TUnit.Data.IProvider p, TUnit.Class.Class obj, TUnit.Class.CCollection Collection)
        {
        }
    }
    #endregion
}
